﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace Rubis.Management.Page_NewsInfo
{
    public partial class News_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDDLType();
                    txtXUHAO.Text = "0";
                    txtNewsDateTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDDLType()
        {
            IList<NewsType> types = NewsTypeManager.GetAllNewsTypes();
            ddlNewsType.Items.Clear();
            ddlNewsType.Items.Add(new ListItem("-", ""));
            foreach (NewsType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.ID.ToString();
                li.Text = type.Title;
                ddlNewsType.Items.Add(li);
            }
        }


        protected void btnUp_Click(object sender, EventArgs e)
        {

            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                lblILogo.Text = FileUploads(this, fuInsertLogo, "..\\..\\UpLoad\\newsImage\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = "../../UpLoad/newsImage/" + lblILogo.Text;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text.Trim() != "")
            {
                NewsInfo photo = new NewsInfo();
                photo.TypeID = Convert.ToInt32(ddlNewsType.SelectedValue);
                photo.Title = txtTitle.Text.Trim();
                photo.NewsContent = FCKeditor1.Value;
                photo.Image = lblILogo.Text;
                photo.NewsDate = Convert.ToDateTime(txtNewsDateTime.Text.Trim() + DateTime.Now.ToString(" HH:mm:ss"));
                photo.Tag = txtTag.Text.Trim();
                photo.XUHAO = Convert.ToInt32(txtXUHAO.Text.Trim());
                photo.TitleColor = txtColor.Text.Trim();
                NewsInfoManager.AddNewsInfo(photo);

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('添加新闻信息成功。');window.opener.location.replace('News_List.aspx');window.close();", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请输入新闻的标题。');", true);
            }

        }

        #region FileUp
        public static string FileUploads(Page page, FileUpload fileProduct, string url)
        {
            if (!fileProduct.HasFile)
            {
                return "出错";

            }
            try
            {
                string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
                fileProduct.SaveAs(page.Server.MapPath(url) + urlimg);
                return urlimg;
            }
            catch
            {
                return "出错";
            }
        }
        #endregion
    }
}
